package org.itboys.member.service;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.member.entity.Member;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
 * 会员
 * @author weisky
 *
 */
@Service
public class MemberService extends BaseService<Member, Long>{

	private static final long serialVersionUID = 1L;
	
	@Resource(name = "memberDS")
	private MongoDataSource ds;
	@Autowired
	private ResourceHolder resourceHolder;
	
	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	/**
	 * 修改或者保存
	 * @param meber
	 * @param logos : 头像
	 * @return -1:账号已存在  0：系统错误  1：成功
	 */
	public int insert(Member member, MultipartFile logos){
		try{
			
			String logo = null;
			if(logos != null && !logos.isEmpty()){
				logo = FileUtils.saveFile(logos.getInputStream(), resourceHolder.getStringValue("fileUploadPath"), logos.getOriginalFilename(), logos.getContentType());
			}
			
			if(member.getId() != 0){//修改
				Member mm = super.getById(member.getId());
				if(StringUtils.isNotBlank(logo)){
					member.setLogo(logo);
				}else{
					member.setLogo(mm.getLogo());
				}
				member.setPassword(mm.getPassword());
				member.setType(mm.getType());
				member.setLevel(mm.getLevel());
				member.setStatus(mm.getStatus());
				update(member);
			}else{//新增
				boolean exist = super.exists("username", member.getUsername());//判断手机号是否已经被注册过了
				if(exist){
					return -1;//手机号（用户名）已存在
				}
				member.setPassword(Digests.md5(member.getPassword()));//将密码转换成MD5加密
				member.setLogo(logo);
				save(member);
			}
			return 1;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("MemberService.insert error", e);
			return 0;
		}
	}
	
	/**
	 * 用于微信的保存
	 * @param member
	 * @return
	 */
	public boolean weixinInsert(Member member){
		try{
			member.setPassword(Digests.md5(member.getPassword()));//将密码转换成MD5加密
			save(member);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("MemberService.weixinInsert error", e);
			return false;
		}
	}
	
	/**
	 * 修改会员状态
	 * @param memberId
	 * @param status
	 * @return
	 */
	public boolean updateStatus(Long memberId, int status){
		try{
			super.update(memberId, "status", status);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("MemberService.updateStatus error", e);
			return false;
		}
	}
	
	/**
	 * 修改密码
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 */
	public int updatePwd(String mobile, String oldPwd, String newPwd){
		try{
			Member member = super.getByField("username", mobile);
			if(member != null){
				if(!StringUtils.equals(Digests.md5(oldPwd), member.getPassword())){//旧密码跟新密码不相同
					return -2;
				}
				super.update(member.getId(), "password", Digests.md5(newPwd));
				return 1;//修改成功
			}
			return -1;//会员不存在
		}catch(Exception e){
			logger.error("MemberService.updatePwd error", e);
			return 0;
		}
	}
	
	/**
	 * 修改密码
	 * @param mobile
	 * @param newPwd
	 * @return
	 */
	public int updatePwd(String mobile, String newPwd){
		try{
			Member member = super.getByField("username", mobile);
			if(member != null){
				super.update(member.getId(), "password", Digests.md5(newPwd));
				return 1;//修改成功
			}
			return -1;//会员不存在
		}catch(Exception e){
			logger.error("MemberService.updatePwd error", e);
			return 0;
		}
	}
}
